#include "contour_file.h"
#include <cstdlib>

#include <list>
#include "types.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

#ifndef NULL
#define NULL 0
#endif

ContourFile::ContourFile(){}

ContourFile::~ContourFile(){}

Contour* ContourFile::loadContour(char *filename){
    Contour *contour;

    FILE *file;

    file = fopen(filename,"r");

    char tag[20];
    char specifier;
    int value,othervalue;
    float floatvalue;


    if (file == NULL)
     {
        cerr << "File could not be opened\n" << endl;
        exit( 12 );
     }

    contour = new Contour;

    cout << "file loaded\n\n";

    while(fscanf(file,"%c",&specifier)==1){
        if(specifier == '#'){
            do{
                fscanf(file,"%c",&specifier);
            }while(specifier!='\n');
            continue;
        }
        if(specifier == '<'){
            fscanf(file,"%s =",tag);
            if(!strncmp(tag,(char*)"closed",6)){
                fscanf(file," %d>\n",&value);
                if(value==0){
                    contour->setClosed(false);
                    continue;
                }
                else{
                    contour->setClosed(true);
                    continue;
                }
            }
            else if(!strncmp(tag,"label",5)){
                fscanf(file," %d>\n",&value);
                contour->setLabel(value);
                continue;
            }
            else if(!strncmp(tag,"pixelsize",9)){
                fscanf(file," %f>\n",&floatvalue);
                contour->setPixelSize(floatvalue);
                continue;
            }
            else if(!strncmp(tag,"pixellenght",11)){
                fscanf(file," %d>\n",&value);
                continue;
            }
            else if(!strncmp(tag,"origin",6)){
                fscanf(file," %d>\n",&value);
                contour->setOrigin(value);
                continue;
            }
            else{
                fscanf(file," %d>\n",&value);
                cout << "TAG error\n";
                continue;
            }
        }
        if(specifier=='['){
            fscanf(file,"POINTS]\n");
            break;
        }
    }
    while(fscanf(file,"%c",&specifier)==1){
        if(specifier=='('){
            fscanf(file,"%d,%d)\n",&value,&othervalue);
            contour->addPoint(value,othervalue);
            continue;
        }
        else
            cout << "format error\n";
    }

    fclose(file);
    return contour;

}

void ContourFile::saveContour(Contour *contour,char *filename){
    FILE *file;
    file = fopen(filename,"w");

    if (file == NULL){
        cerr << "File could not be opened\n" << endl;
        exit( 12 );
    }

    fprintf(file,"<label = %d>\n",contour->getLabel());
    fprintf(file,"<closed = %d>\n",contour->isClosed());
    fprintf(file,"<pixellenght = %d>\n",contour->getPixelLenght());
    fprintf(file,"<pixelsize = %f>\n",contour->getPixelSize());
    fprintf(file,"<origin = %d>\n",contour->getOrigin());
    fprintf(file,"[POINTS]\n");
    list<Position>::iterator i = contour->points.begin();
    while(i!=contour->points.end()){
        fprintf(file,"(%d,%d)\n",(*i).x,(*i).y);
        i++;
    }
    fclose(file);
}


